package com.changer.common.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.changer.common.service.CacheService;
import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;

/**
 * @author Jiwei Guo
 * 
 */
public class MemcacheServiceImpl implements CacheService {

	private static final Logger LOGGER = LoggerFactory.getLogger(MemcacheServiceImpl.class);

	private MemCachedClient client;

	private List<String> servers;

	private int initConn = 5;

	private int minConn = 5;

	private int maxConn = 100;

	private int readTimeout = 3;
	
	public MemcacheServiceImpl() {
		//NOP
	}

	@PostConstruct
	public void init() {
		client = new MemCachedClient();
		SockIOPool pool = SockIOPool.getInstance();
		pool.setServers(servers.toArray(new String[] {}));
		pool.setFailover(true);
		pool.setInitConn(this.getInitConn());
		pool.setMinConn(this.getMinConn());
		pool.setMaxConn(this.getMaxConn());
		pool.setNagle(false);
		pool.setSocketTO(this.getReadTimeout());
		pool.initialize();
		String config = "initConn " + this.getInitConn() + " , minConn " + this.getMinConn() + " , maxConn " + this.getMaxConn() + " , socketReadTime : " + this.getReadTimeout();
		LOGGER.debug("initialize the memcache server : " + this.getStringServers() + " with config : " + config);
	}

	public int getInitConn() {
		return initConn;
	}

	public void setInitConn(int initConn) {
		this.initConn = initConn;
	}

	public int getMinConn() {
		return minConn;
	}

	public void setMinConn(int minConn) {
		this.minConn = minConn;
	}

	public int getMaxConn() {
		return maxConn;
	}

	public void setMaxConn(int maxConn) {
		this.maxConn = maxConn;
	}

	public int getReadTimeout() {
		return readTimeout;
	}

	public void setReadTimeout(int readTimeout) {
		this.readTimeout = readTimeout;
	}

	public Object get(String key) {
		return this.client.get(key);
	}

	public void put(String key, Object value) {
		this.client.setPrimitiveAsString(true);
		this.client.set(key, value);
	}

	public void setServers(List<String> servers) {
		this.servers = servers;
	}

	public boolean remove(String key) {
		return this.client.delete(key);
	}

	public void put(String key, Object value, Date expire) {
		this.client.set(key, value, expire);
	}

	private String getStringServers(){
		StringBuilder sb = new StringBuilder();
		List<String> servers = this.servers;
		for(String server : servers){
			sb.append(server).append(",");
		}
		return "[ " + sb.toString() + " ]";
	}

	public void incr(String key) {
		this.client.incr(key);
	}

	public void rpush(String key, String value) {
		throw new UnsupportedOperationException("Mecached不支持此操作！");
	}

	public List<String> getRpush(String key) {
		throw new UnsupportedOperationException("Mecached不支持此操作！");
	}

	public boolean exists(String key) {
		return this.client.keyExists(key);
	}

	public Set keys(String prefix) { throw new UnsupportedOperationException("Mecached不支持此操作！"); }

	public boolean removeKeys(Set key) { throw new UnsupportedOperationException("Mecached不支持此操作！");	}

}
